import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Login from '@/pages/Login/Login.vue'
import Main from '@/views/Main/Main.vue'

// 重写push
let originPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve || reject) {
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
}
// 重写replace
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve || reject) {
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
}

Vue.use(VueRouter)
const router = new VueRouter({
    routes: [
        {
            path: '/login',
            name: 'login',
            component: Login,
            meta: {
                title: '无人船后台管理登录页',
            }
        },
        {
            path: '/404',
            name: 'NotFound',
            component: () => import('@/pages/featrue/404.vue'),
            meta: {
                title: '404',
            }
        },
    ]
})

function routesHandler(data) {
    return data.map(item => {
        if (item.childrens) {
            return routesHandler(item.childrens);
        } else {
            const comName = item.component;//这里必须用变量做中间存储
            item.component = () => import(`@/pages/${comName}/${comName}.vue`);
            return item
        }
    })
}
// 默认进入页面
const defaultPage = 'login';

// 路由守卫
router.beforeEach((to, from, next) => {
    let token = store.state.userHandle.token;
    if (!token) {//如果token不存在则需要跳转到登录页面
        if (to.path == ('/'+defaultPage)) {
            return next();
        } else {
            return next({
                path: defaultPage,
                query: { redirect: to.fullPath }//把跳转的路由path作为重定向的参数，是登录成功后跳转的目的。
            })
        }
    } else {//已登录的情况下，除了login都可以进入
        let able = store.state.menuSwitch.menus.length;
        if (able === 0) {
            store.dispatch('menuSwitch/getUserMenu').then(res => {
                const uio = routesHandler(res);
                router.addRoute({
                    path: '/',
                    component: Main,
                    redirect: '/cruisemanagement',
                    children: uio.flat()
                });
                router.addRoute({
                    path: '*',
                    redirect: { name: 'NotFound' }
                });
                next({ ...to, replace: true });
            })
        } else {
            // 内部页面需要先加载菜单之后才能进入
            store.commit('menuSwitch/addTab', {
                name: to.name,
                meta: to.meta
            })
            if (to.path == '/login') {
                return next({ name: 'home' });
            }
            return next();
        }
    }
})
//设置浏览器页签标题
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next();
})
export default router